﻿//-----------------------------------------------------------------------
// <copyright file="Customer.cs" company="SoSoft">
//     Copyright (c) SoSoft . All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace FirstSample
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Text;

    /// <summary>
    /// 用来表示顾客，它也拥有数据和相应的访问函数.
    /// Use to show Customer.
    /// </summary>
    public class Customer
    {
        /// <summary>
        /// Initializes a new instance of the Customer class.
        /// </summary>
        /// <param name="name">customer name</param>
        public Customer(string name)
        {
            this.Name = name;
            this.Rentals = new List<Rental>();
        }

        /// <summary>
        /// Gets or sets Name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets Rentals
        /// </summary>
        public List<Rental> Rentals { get; set; }

        /// <summary>
        /// add a new rental record
        /// </summary>
        /// <param name="newRental">new rental record</param>
        public void AddRental(Rental newRental)
        {
            this.Rentals.Add(newRental);
        }

        /// <summary>
        /// Export the statement.
        /// </summary>
        /// <returns>statement content</returns>
        public string Statement()
        {
            StringBuilder contentBuilder = new StringBuilder();

            contentBuilder.AppendFormat(
                                        CultureInfo.CurrentCulture,
                                        "Rental Record for {0} {1}",
                                        this.Name,
                                        Environment.NewLine);

            foreach (Rental currentRental in this.Rentals)
            {
                // show figuresfor this rental 显示此笔租借记录
                contentBuilder.AppendFormat(
                                            CultureInfo.CurrentCulture,
                                            "\t {0} \t {1} {2}",
                                            currentRental.Movie.Title,
                                            currentRental.GetCharge(),
                                            Environment.NewLine);

            }

            // Add footer lines 结尾打印
            contentBuilder.AppendFormat(
                                        CultureInfo.CurrentCulture,
                                        "Amount ownd is {0} {1} You earned {2} frequent renter points",
                                        this.GetTotalCharge(),
                                        Environment.NewLine,
                                        this.GetFrequentRenterPoints());

            return contentBuilder.ToString();
        }


        public string HtmlStatement()
        {
            StringBuilder HtmlContentBuilder = new StringBuilder();

            HtmlContentBuilder.AppendFormat(
                                        CultureInfo.CurrentCulture,
                                        "<H1>Rental Record for <EM>{0}</EM><H1><P>{1}",
                                        this.Name,
                                        Environment.NewLine);

            foreach (Rental currentRental in this.Rentals)
            {
                // show figuresfor this rental 显示此笔租借记录
                HtmlContentBuilder.AppendFormat(
                                            CultureInfo.CurrentCulture,
                                            "\t {0} \t {1} <BR>{2}",
                                            currentRental.Movie.Title,
                                            currentRental.GetCharge(),
                                            Environment.NewLine);
            }

            // Add footer lines 结尾打印
            HtmlContentBuilder.AppendFormat(
                                        CultureInfo.CurrentCulture,
                                        "<P>Amount ownd is <EM>{0}</EM><P>{1} You earned <EM>{2}</EM> frequent renter points<P>",
                                        this.GetTotalCharge(),
                                        Environment.NewLine,
                                        this.GetFrequentRenterPoints());

            return HtmlContentBuilder.ToString();
        }

        /// <summary>
        /// Get total charge.
        /// </summary>
        /// <returns>Total charge</returns>
        private double GetTotalCharge()
        {
            // 总消费金额
            double totalAmount = 0;

            foreach (Rental currentRental in this.Rentals)
            {
                totalAmount += currentRental.GetCharge();
            }

            return totalAmount;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private int GetFrequentRenterPoints()
        {
            // 常客返点
            int frequentRenterPoints = 0;

            foreach (Rental currentRental in this.Rentals)
            {
                frequentRenterPoints += currentRental.GetFrequentRenterPoints();
            }

            return frequentRenterPoints;
        }
    }
}